package com.eregg.learn.netty.protocol.util;

import com.eregg.learn.netty.protocol.attribute.Attributes;
import com.eregg.learn.netty.protocol.session.Session;
import io.netty.channel.Channel;
import io.netty.util.Attribute;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SessionUtil {

    private static final Map<String, Channel> userIdChannelMap = new ConcurrentHashMap<>();


    public static boolean hasLogin(Channel channel){

        return channel.hasAttr(Attributes.SESSION);
    }

    public static void bindSession(Session session, Channel channel){

            userIdChannelMap.put(session.getUserId(), channel);
            channel.attr(Attributes.SESSION).set(session);

    }

    public static void unBindSession(Channel channel){
        if (hasLogin(channel)){
            userIdChannelMap.remove(getSession(channel).getUserId());
            channel.attr(Attributes.SESSION).set(null);
        }
    }

    public static Session getSession(Channel channel){
        return channel.attr(Attributes.SESSION).get();
    }

    public static Channel getChannel(String userId){

        return userIdChannelMap.get(userId);
    }

}
